﻿using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Event;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.Event.Core;
using CPPEI.Coolzon.Theme;
using Coolvis.Console.Theme.MainViewControl;

namespace Coolvis.Console.Theme
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : CPPEI.Coolzon.UI.View, IMainView
    {
        SelectSceneView _viewSelectScene;

        private Scene _scene;
        public MainView()
        {
            InitializeComponent();
            ExtentionFunctionLoader.Instance.Load(string.Format("{0}/config/extentionfunction.xml", AppDomain.CurrentDomain.BaseDirectory));
            
        }
        protected override void OnActive()
        {
            base.OnActive();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            sceneContent.Child = renderView;


            _viewSelectScene = new SelectSceneView();
            CPPEI.Coolzon.Common.EventBus.Instance.Register(_viewSelectScene);
            CoreEventBus.Register(_viewSelectScene);

            // 将事件远程处理模式的代理实例注册到事件总线上
            RemoteEventProxy.Instance.SetRenderView(renderView);

            CPPEI.Coolzon.Common.EventBus.Instance.Register(header);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(sceneContent);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(operationNavigation);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(jsControl);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(zoomView);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(this);

            NCLEventBus.Register(header);
            NCLEventBus.Register(sceneContent);
            NCLEventBus.Register(operationNavigation);
            NCLEventBus.Register(jsControl);
            NCLEventBus.Register(zoomView);
            NCLEventBus.Register(this);

            UIEventBus.Register(header);
            UIEventBus.Register(sceneContent);
            UIEventBus.Register(operationNavigation);
            UIEventBus.Register(jsControl);
            UIEventBus.Register(zoomView);
            UIEventBus.Register(this);

            CoreEventBus.Register(header);
            CoreEventBus.Register(sceneContent);
            CoreEventBus.Register(operationNavigation);
            CoreEventBus.Register(jsControl);
            CoreEventBus.Register(zoomView);
            CoreEventBus.Register(this);

            CPPEI.Coolzon.Common.EventBus.Create(CPPEI.Coolzon.ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Register(renderView);

            OpenDefaultSceneThread.Start(UIContext.Current.Solution);
        }

        protected override void OnDeactive()
        {
            // 清空场景选择界面上的按钮,因为按钮上关联了场景Metadata
            _viewSelectScene.Clear();
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(_viewSelectScene);
            CoreEventBus.Unregister(_viewSelectScene);

            this.zoomView.Close();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            sceneContent.Child = null;
            // 从事件总线撤销注册
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(renderView);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(header);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(sceneContent);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(operationNavigation);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(jsControl);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(zoomView);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(this);

            NCLEventBus.Unregister(renderView);
            NCLEventBus.Unregister(header);
            NCLEventBus.Unregister(sceneContent);
            NCLEventBus.Unregister(operationNavigation);
            NCLEventBus.Unregister(jsControl);
            NCLEventBus.Unregister(zoomView);
            NCLEventBus.Unregister(this);

            NCLEventBus.Unregister(renderView);
            UIEventBus.Unregister(header);
            UIEventBus.Unregister(sceneContent);
            UIEventBus.Unregister(operationNavigation);
            UIEventBus.Unregister(jsControl);
            UIEventBus.Unregister(zoomView);
            UIEventBus.Unregister(this);

            CoreEventBus.Unregister(header);
            CoreEventBus.Unregister(sceneContent);
            CoreEventBus.Unregister(operationNavigation);
            CoreEventBus.Unregister(jsControl);
            CoreEventBus.Unregister(zoomView);
            CoreEventBus.Unregister(this);

            CPPEI.Coolzon.Common.EventBus.Create(CPPEI.Coolzon.ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(renderView);

            base.OnDeactive();
        }



        private void closeImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CPPEI.Coolzon.Common.EventBus.Instance.Publish(new CloseNavEvent());
        }

        private void OnBtnCloseMagnifierClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowMagnifierEvent(false));
        }
        private void OnBtnCloseLaserPointClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowLaserPointEvent(false));
        }

        /// <summary>
        /// 打开第一个场景
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnGoToPrevScene_Click(object sender, RoutedEventArgs e)
        {
            if (UIContext.Current.OpenedSceneList != null && UIContext.Current.OpenedSceneList.Count >= 2)
            {
                //先取出当前场景，再取出上一场景
                UIContext.Current.OpenedSceneList.Pop();
                string preSceneName = UIContext.Current.OpenedSceneList.Pop();
                Scene preScene = SolutionUtil.GetScene(UIContext.Current.Solution, preSceneName);
                NCLEventBus.Publish(new NCLOpenSceneEvent(SwitchMode.Replace, "", preScene.Name, null));
            }
        }

        /// <summary>
        /// 打开下一个场景
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnGoToNextScene_Click(object sender, RoutedEventArgs e)
        {
            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            ScenePart sceneInst = renderView.GetCurrentPart() as ScenePart;
            if (sceneInst != null)
            {
                Scene scene = (sceneInst as ScenePart).SceneMatadata;
                Scene nextScene = SolutionUtil.GetNextDisplayflowNode(UIContext.Current.Solution, scene);
                if (nextScene == null)
                {
                    return;
                }
                OpenSceneArgs openSceneArgs = new OpenSceneArgs();
                openSceneArgs.ParametersString.Add("sys:isresetviewstate", "true");
                NCLEventBus.PublishAsync(
                    new NCLOpenSceneEvent(SwitchMode.Replace, "", nextScene.Name, openSceneArgs));
            }
        }

        #region tool button
        private bool _isShowLaser = false;
        private bool _isShowMagnifier = false;
        private void ctlBtnLaser_Click(object sender, RoutedEventArgs e)
        {
            if (_isShowLaser)
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
            else
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
                UIEventBus.Publish(new UIShowLaserPointEvent(true));
            }
        }

        private void ctlBtnMagnifier_Click(object sender, RoutedEventArgs e)
        {
            if (_isShowMagnifier)
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
            else
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(true));
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
        }

        private void ctlBtnResetSolution_Click(object sender, RoutedEventArgs e)
        {
            CPPEI.Coolzon.Common.EventBus.Instance.Publish(new CPPEI.Coolzon.UI.Views.Events.BeginResetSolutionEvent(UIContext.Current.SolutionName));
        }
        #endregion
    }
}
